package com.navi.loaddata.loadService.impl.cellMappingData;

import com.navi.loaddata.core.gpEntity.hisToGP.ProductHis;
import com.navi.loaddata.core.mesEntity.MesP1MESDB;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.oracleEntity.fileMapping.Cell2ChipMappingT;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import com.navi.loaddata.loadDao.SaveMappingDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class LoadCell2MappingDataService {
    private static final Logger logger = LoggerFactory.getLogger(LoadCell2MappingDataService.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());


    @Autowired
    private ProcessCell2MappingDataService processCell2MappingDataService;
    @Autowired
    private LoadCell2MappingInfoService loadCell2MappingInfoService;
    @Autowired
    private SaveMappingDao saveMappingDao;

    public String[] loadCell2MappingDataMain(EtlConfD etlConfD) throws Exception {
        String[] resMsg = loadCell2MappingData(etlConfD);
        logger.info("LoadCell2MappingDataMain finish");
        return resMsg;
    }


    private String[] loadCell2MappingData(EtlConfD etlConfD) throws Exception {
        String[] resMsg = new String[3];

        List<MesP1MESDB> glassModelList = loadCell2MappingInfoService.loadModelInfo();

        HashMap<String, String> modelMap = new HashMap();
        glassModelList.forEach(model -> {
            modelMap.put(model.getGlassModel() + "_" + model.getTftNo(), model.getCfNo());
        });


        // get L4100 glass
        List<ProductHis> glassList = loadCell2MappingInfoService.loadCell2Info(etlConfD);

        logger.info("{} L4100 glass count {}", etlConfD.getShop(), glassList.size());
        if (!glassList.isEmpty()) {
            //process
            List<Cell2ChipMappingT> cellList = processCell2MappingDataService.processCell2Mapping(etlConfD, glassList, modelMap);

            resMsg[0] = String.valueOf(glassList.size());
            resMsg[1] = "0";
            //save
            for (int i = 0; i < cellList.size(); i += 1000) {
                List<Cell2ChipMappingT> cellList1000 = new ArrayList<>();
                StringBuffer glassIds = new StringBuffer();
                for (int j = 0; j < 1000; j++) {
                    if (i + j < cellList.size()) {
                        glassIds.append("'" + cellList.get(i + j).getChipId() + "',");
                        cellList1000.add(cellList.get(i + j));
                    }
                }
                glassIds.deleteCharAt(glassIds.length()-1);

                saveMappingDao.saveCellMapping(cellList1000,glassIds);
            }
        }
        return resMsg;
    }


}
